# -*- coding: utf-8 -*-
# 姓名:谚语
# 开发时间:2022/10/29 8:14
from datetime import datetime

from comment.utils.Serializers import BasePaginateSerializer, BaseListSerializer


class InvestRecordPaginateSerializer(BasePaginateSerializer):
    """投资记录列表的分页序列化"""

    def get_object(self, obj):
        return {
            'pId': obj.pId,
            'plan_name': obj.product.productName,
            'invest_amount': float(obj.pAmount),
            "yearRate": float(obj.pEarnings),
            "totalIncome": float(obj.pMonthInterest),
            'month_Income': float(obj.pMonthlyExtractInterest),
            'deal_date': obj.pBeginDate.strftime("%Y-%m-%d"),  # 日期类型格式化: .strftime(格式)
            # 'period': obj.pDeadlineAsDay / 30,  # 期数  PDeadlineAsDay 投资天数
            'period': obj.pDeadline,
            'status': obj.pStatus,
            'remark': obj.pRemark
        }


class DealRecordPaginateSerializer(BasePaginateSerializer):
    """交易记录列表的分页序列化"""

    def get_object(self, obj):
        return {
            'deal_date': obj.aDate.strftime("%Y-%m-%d"),
            'deal_type': obj.aType,
            "descrp": obj.aDescreption,
            "deal_amount": float(obj.aAmount),
            'aAfter_Money': float(obj.aAfterTradingMoney),
            'deal_status': '交易成功' if obj.aTransferStatus else '交易失败',
            'user_name': obj.user.username
        }


class DebtPaginateSerializer(BasePaginateSerializer):
    """债权的分页序列化"""

    def get_object(self, obj):
        return {
            'debtNo': obj.debtNo,  # 当前债权的编号
            'loanNo': obj.loanNo,  # 借款编号
            "loanStartDate": obj.loanStartDate.strftime("%Y-%m-%d"),  # 借款开始的时间
            "repaymenDate": obj.repaymenDate,  # 还款的时间(债权转入时间)
            'debtYearRate': float(obj.debtYearRate),  # 债权的年利率
            'debtMoney': float(obj.debtMoney),  # 债权金额
            'debtOriginalMoney': float(obj.repaymenMoney),  # 债权的转入金额(剩余匹配到的金额)
            'matchedStatus': obj.matchedStatus,  # 匹配状态
            'matchedMoney': float(obj.matchedMoney),  # 已匹配金额
            'debtStatus': obj.matchedStatus,  # 匹配状态
        }


class LoanPaginateSerializer(BasePaginateSerializer):
    """ 借款列表分页序列化"""

    def get_object(self, obj):
        return {
            'loanApplyId': obj.id,  # 借款的ID号
            'name': obj.lName,  # 借款人的真实姓名
            "applyTime": obj.applyDate.strftime("%Y-%m-%d %H:%M:%S"),  # 借款的开始时间
            "amount": float(obj.loanNum),  # 借款金额
            'duration': obj.duration,  # 借款时长
            'loanRate': float(obj.lRate),  # 利率
            'repayDay': obj.lRepayDay,  # 还款时间
            'repayType': obj.lRepayType,  # 还款的类型
            'status': obj.status,  # 审核状态
            'debt_match_status': obj.debt_info.matchedStatus if obj.debt_info else '',  # 债权状态
            'debt_id': obj.debt_info.id if obj.debt_info else '',  # 债权的ID

        }


class RepayPlanSerializer(BaseListSerializer):
    """还款计划的序列化"""

    def to_dict(self):
        list = []
        for obj in self.data_list:
            list.append(
                {
                    'id': obj.id,  # 还款id
                    'currentTerm': obj.currentTerm,  # 当前还款期
                    'receivableDate': obj.receivableDate.strftime("%Y-%m-%d"),  # 还款日期
                    "receivableMoney": float(obj.receivableMoney),  # 应还余额
                    'isReturned': obj.isReturned,  # 是否还款
                }
            )
        return list


class LoanApplyListSerializer(BaseListSerializer):
    """借款申请列表的序列化"""

    def to_dict(self):
        list = []
        for obj in self.data_list:
            list.append(
                {
                    'loanApplyId': obj.id,
                    'name': obj.lName,
                    "applyTime": obj.applyDate.strftime("%Y-%m-%d %H:%M:%S"),
                    "amount": float(obj.loanNum),
                    'duration': obj.duration,
                    'loanRate': float(obj.lRate),
                    'repayDay': obj.lRepayDay,
                    'repayType': obj.lRepayType,
                    'status': obj.status,
                    'matchedStatus': obj.debt_info.matchedStatus if obj.debt_info else None
                }
            )
        return list


class AllMatchedPaginateSerializer(BasePaginateSerializer):
    """所有的待匹配 和 已经匹配资金的序列化"""

    def get_object(self, obj):
        return {
            'weigh': 0,  # 权重
            'username': obj.investRecord.user.username,  # 用户名
            'InvestRecordNum': obj.investRecord.pSerialNo,  # 投资编号
            'productName': obj.investRecord.product.productName,  # 产品名称
            'investDate': obj.investRecord.pDate.strftime("%Y-%m-%d"),  # 投资日期
            'period': obj.investRecord.pDeadlineAsDay,  # PDeadlineAsDay 投资天数
            'notMatchedMoney': float(obj.fNotMatchedMoney),  # 待匹配金额
            'matchStatus': obj.matchedStatus,  # 状态
        }


class ExpectedReturnPaginateSerializer(BasePaginateSerializer):
    """ 当前用户的所有预期收益列表 """

    def get_object(self, obj):
        return {
            'return_id': obj.id,  # 主键
            'userId': obj.userId,  # 用户ID
            'productId': obj.product.productName,  # 产品名称
            "investRcordID": obj.investRecord,  # 投资记录的id
            "expectedDate": obj.expectedDate.strftime("%Y-%m-%d"),
            # date 日期的 时间戳
            'expectedStamp': datetime.timestamp(datetime(obj.expectedDate.date().year, obj.expectedDate.date().month,
                                                         obj.expectedDate.date().day)),
            'return_Money': float(obj.expectedMoney),
        }


class MatchedResultPaginateSerializer(BasePaginateSerializer):
    """匹配结果列表，分页 序列化"""

    def get_object(self, obj):
        return {
            'userId': obj.userId,  # 用户id
            'debtId': obj.debtId,  # 债权id
            'investId': obj.investId,  # 投资记录id
            'transNo': obj.transferSerialNo,  # 匹配结果的流水号
            'Money': float(obj.purchaseMoney),  # 匹配的金额
            'matchDate': obj.matchDate.strftime("%Y-%m-%d %H:%M:%S")  # 匹配的时间
        }
